home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / array.lha / array / array.H < prev    next >
Text File  |  1993-08-08  |  3KB  |  75 lines

  1. / This is a simple fixed array with arbitrary integer lower and upper bounds
  2. / and index range checking.
  3. / The array class uses assertions to check various error conditions,
  4. / including index out-of-range and failed memory allocation. 
  5. / Failed assertions cause program termination, and posssibly a core dump.
  6. /
  7. / The element type must have a default constructor
  8. / (a constructor that takes no arguments) or be of a type that does
  9. / not need a constructor, such as, integers and other built-in types.
  10. /
  11. / The copy-constructor of the array should use the copy-constructor of the
  12. / element type to initialize its elements.  Unfortunately, this doesn't
  13. / work (due to a compiler bug?), so there is no copy-constructor for arrays.
  14. /
  15. / It must also be possible to assign to the element type, and to
  16. / destroy elements.
  17. /
  18. / Author: Dag Bruck, Department of Automatic Control, Lund Institute of
  19. / Technology, Box 188, S-221 00 Lund, Sweden.  E-mail: dag@control.lth.se
  20. /
  21. / $Id: array.H,v 1.4 1992/07/07 12:38:28 dag Exp $
  22.  
  23. ifndef ARRAY_H
  24. define ARRAY_H
  25.  
  26. emplate <class T>
  27. lass Array {
  28. ublic:
  29.   Array(int lower_bound, int upper_bound);
  30.   // Creates an array with given lower and upper bounds.  The upper
  31.   // bound must be greater than the lower bound.  The elements of the
  32.   // array are initialized by the default constructor of the element type.
  33.  
  34.   ~Array();
  35.   // Destroys the array and its elements.
  36.  
  37.   T& operator [] (int);
  38.   const T& operator [] (int) const;
  39.   // Returns (a reference to) an element of the array.  The index must
  40.   // be in the array's bounds.
  41.  
  42.   Array<T>& operator = (const T &);
  43.   // Assigns a new value to all elements of the array.
  44.  
  45.   Array<T>& operator = (const Array<T> &);
  46.   // Assigns one array to another.  The bounds of the arrays
  47.   // must be the same.
  48.  
  49.   void bounds(int& lower_bound, int& upper_bound) const;
  50.   // Returns the lower and upper bounds of the array, as given
  51.   // to the constructor.
  52.  
  53.   unsigned size() const { return sz; }
  54.   // Returns the number of elements in the array.
  55.  
  56.   T* storage() { return data; }
  57.   // Returns a pointer to the internal data space of the array.
  58.   // The internal data space is never re-allocated during the lifetime
  59.   // of the array.
  60.   //
  61.   // Using this operation is potentially dangerous because it defies
  62.   // index range checking, and the pointer may be left dangling
  63.   // after the array has been destroyed.
  64.  
  65. rivate:
  66.   T* data;
  67.   const int low;
  68.   const unsigned sz;
  69.  
  70.   Array(const Array<T> &);
  71.   // No copy-constructor.
  72. ;
  73.  
  74. endif
  75.